home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / mg2a_src.zip / SYS / PRIME / TTYIO.C < prev   
C/C++ Source or Header  |  1988-08-23  |  5KB  |  202 lines

  1. /*
  2.  *     sys>prime>ttyio.c by Robert A. Larson
  3.  *
  4.  * The functions in this file
  5.  * negotiate with the operating system for
  6.  * keyboard characters, and write characters to
  7.  * the display in a barely buffered fashion.
  8.  */
  9. #include       "def.h"
  10.  
  11. #define           NOBUF   512           /* Output buffer size.           */
  12.  
  13. char   obuf[NOBUF];               /* Output buffer.           */
  14. short  nobuf;                   /* characters in obuf           */
  15. int    nrow;                   /* Terminal size, rows.           */
  16. int    ncol;                   /* Terminal size, columns.      */
  17. short  ospeed;                   /* Terminal speed, for termlib.l */
  18.  
  19. #ifndef PRE21
  20. /* use undocumented calls to set terminal modes */
  21. #define NSETTINGS 6
  22. fortran void as$lin(), as$get(), as$set();
  23. static short myline = 0;
  24. static short reset[NSETTINGS][2];
  25. #else
  26. /* do the best we can without undocumented calls */
  27. fortran short duplx$();
  28. fortran void break$();
  29. short duplx;
  30. #endif
  31.  
  32. /*
  33.  * This function gets called once, to set up
  34.  * the terminal channel.
  35.  */
  36.  
  37. ttopen()
  38. {
  39. #ifndef PRE21
  40.     short code, vers, len, bad;
  41.     static short settings[NSETTINGS][2] = {
  42.     {1, 0},                   /* no echo */
  43.     {2, 0},                   /* don't echo line feed for cr */
  44.     {3, 0},                   /* disable ^S/^Q processing */
  45.     {11, 0},               /* parity: none */
  46.     {12, 3},               /* character length: 8 */
  47.     {15, 1},               /* tran protocol */
  48.     };
  49.     short plist[38][2];
  50.     short *p;
  51.     register int i;
  52.  
  53.     if(myline==0) {
  54.     as$lin(myline, code);
  55.     if(code!=0) panic("Can't get terminal line");
  56.     /* do io to get terminal type before setting modes */
  57.     (void) gettermtype();
  58.     }
  59.     p = &plist[0][0];
  60.     as$get(myline, vers, p, len, code);
  61.     if(code!=0) panic("Can't determine current settings");
  62.     ospeed = plist[9-1][1];
  63.     for(i=0; i < NSETTINGS; i++) {
  64.     reset[i][0] = settings[i][0];
  65.     reset[i][1] = plist[settings[i][0]-1][1];
  66.     }
  67.     p = &settings[0][0];
  68.     as$set(myline, (short)0, p, (short)NSETTINGS, code, bad);
  69.     if(code!=0) panic("Can't set terminal modes");
  70. #else
  71.     (void) gettermtype();
  72.     duplx = duplx$((short)-1);
  73.     (void) duplx$((unsigned short)0140000);
  74.     ospeed = 4; /* assume 9600 for debugging */
  75. #endif
  76.     nobuf = 0;
  77. }
  78.  
  79. /*
  80.  * This function gets called just
  81.  * before we go back home to the shell. Put all of
  82.  * the terminal parameters back.
  83.  */
  84. ttclose()
  85. {
  86. #ifndef PRE21
  87.     short code, bad;
  88.     short *p;
  89. #endif
  90.  
  91.     ttflush();
  92. #ifndef PRE21
  93.     p = &reset[0][0];
  94.     as$set(myline, (short)0, p, (short)NSETTINGS, code, bad);
  95.     if(code!=0) {
  96.     printf("Can't reset terminal modes: code: %d bad: %d\n",
  97.         code, bad);
  98.     exit(1);
  99.     }
  100. #else
  101.     (void) duplx$(duplx);
  102. #endif
  103. }
  104.  
  105. /*
  106.  * Write character to the display.
  107.  * Characters are buffered up, to make things
  108.  * a little bit more efficient.
  109.  */
  110. ttputc(c)
  111. {
  112.        if (nobuf >= NOBUF)
  113.            ttflush();
  114.        obuf[nobuf++] = c;
  115. }
  116.  
  117. /*
  118.  * Flush output.
  119.  */
  120. ttflush()
  121. {
  122.        fortran void tnoua();
  123.  
  124.        if (nobuf != 0) {
  125.            tnoua((char [])obuf, nobuf);
  126.            nobuf = 0;
  127.        }
  128. }
  129.  
  130. /*
  131.  * Read character from terminal.
  132.  * Parity bit is stripped, to be normal
  133.  */
  134. ttgetc()
  135. {
  136.     fortran void t1in();
  137.     short c;
  138.  
  139.     t1in(c);
  140. #ifdef DO_METAKEY
  141.     if(c&0200) c |= METABIT;
  142. #endif
  143.     c |= 0200;
  144. #ifdef PRE21
  145.     if(c=='\n') return '\r';       /* fix repping done by primos */
  146. #ifdef DO_METAKEY
  147.     if(c==('\n'|METABIT)) return '\r'|METABIT;
  148. #endif
  149. #endif
  150.     return c;
  151. }
  152.  
  153. int typeahead()
  154. {
  155.     fortran short tty$in();
  156.  
  157.     return tty$in() < 0;
  158. }
  159.  
  160. panic(s) char *s; {
  161.   fortran void tnoua(), tnou();
  162.  
  163.   ttclose();
  164.   tnoua((char [])"Panic: ", (short)7);
  165.   tnou((char [])s, (short)strlen(s));
  166.   exit(1);
  167. }
  168.  
  169. #ifndef NO_DPROMPT
  170. ttwait() {
  171.   register short i = 20;
  172.   fortran short tty$in();
  173.   fortran void sleep$();
  174.  
  175.   while(i--) {
  176.     if(tty$in()<0) return FALSE;
  177.     sleep$((long)100);
  178.   }
  179.   return tty$in() >= 0;
  180. }
  181. #endif
  182.  
  183. char *gettermtype()
  184. {
  185.     register char *cp;
  186.     char *gvget();
  187.     static char termtype[64] = "";
  188.  
  189.     if(termtype[0]!='\0') return termtype;
  190.     cp = gvget(".TERMINAL_TYPE$");     /* get terminal type */
  191.     if(cp==NULL) cp = gvget(".TERM");
  192.     if(cp != NULL) {
  193.     strncpy(termtype, cp, 64);
  194.     return termtype;
  195.     }
  196.     fputs("Terminal type? ", stdout);
  197.     fgets(termtype, 64, stdin);
  198.     putchar('\n');
  199.     termtype[strlen(termtype)-1] = '\0';    /* chop off \n */
  200.     return termtype;
  201. }
  202.